package com.itboye.textileexpres.order;

import java.util.Observable;
import java.util.Observer;

import android.R.integer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;

import com.itboye.textileexpes.beans.MyOrderBeans;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.alipaytest.ActivityPaymentMethod;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.ClickListener;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.widget.XListView;
import com.itboye.textileexpres.widget.XListView.IXListViewListener;

/**
 * @Name 代付款
 * @author itboye
 * 
 */
public class FragmentMyOrder extends BaseFragment implements Observer,
		ClickListener, IXListViewListener {
	int i;
	// TextView tv;
	XListView mylist;
	MyOrderAdater adapter;
	MyOrderPresenter myOrderPresenter;
	MyOrderBeans beans;
	int page_size = 10;
	int page_inedex = 1;
	private LinearLayout linearyoutIsnull;

	public FragmentMyOrder(int i) {
		// TODO Auto-generated constructor stub
		this.i = i;
	}

	@Override
	public int initView() {

		return R.layout.fragment_myorder;
	}

	@Override
	public void onMyActivityCreated() {
		myOrderPresenter = new MyOrderPresenter(this);
		beans = new MyOrderBeans();
		beans.setUid(IsMobileNumPhone.isUid());
		beans.setQuery_status("1");
		page_size++;
		myOrderPresenter.getInfo(beans);
		mylist.setPullLoadEnable(true);
		mylist.setPullRefreshEnable(true);
		mylist.setXListViewListener(this);
		IntentFilter newshead = new IntentFilter("updateorder");
		getActivity().registerReceiver(avartBroadcastReceiver, newshead);

		IntentFilter UpdateisOrder = new IntentFilter("orderUpdate");
		getActivity().registerReceiver(UpdateOrder, UpdateisOrder);
	}

	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			onStarOrder();

		};
	};

	BroadcastReceiver UpdateOrder = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			onStarOrder();

		};
	};

	public void onStarOrder() {
		beans = new MyOrderBeans();
		beans.setUid(IsMobileNumPhone.isUid());
		beans.setQuery_status("1");
		beans.setPage_index("1");
		beans.setPage_size(page_size + "");
		myOrderPresenter.getInfo(beans);
	}

	@Override
	public void update(Observable observable, Object data) {

		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == myOrderPresenter.Fenye_success) {
				beans = (MyOrderBeans) entity.getData();
				if (beans.getList().size() <= 0) {
					linearyoutIsnull.setVisibility(View.VISIBLE);
					mylist.setVisibility(View.GONE);
				} else if (beans.getList() == null) {
					linearyoutIsnull.setVisibility(View.VISIBLE);
					mylist.setVisibility(View.GONE);
				} else {
					adapter = new MyOrderAdater(getActivity(), beans, this);
					mylist.setAdapter(adapter);
					try {
						closeProgressDialog();
					} catch (Exception e) {
					}
					onIsStop();
				}
			} else if (entity.getEventType() == myOrderPresenter.Fenye_fail) {
				onIsStop();
			} else if (entity.getEventType() == myOrderPresenter.Btn_success) {
				// beans=(MyOrderBeans) entity.getData();
				toast(entity.getData() + "");
				beans.setPage_size(page_size + "");
				beans.setUid(IsMobileNumPhone.isUid());
				myOrderPresenter.getInfo(beans);
				adapter.notifyDataSetChanged();

			} else if (entity.getEventType() == MyOrderPresenter.Delete_success) {
				beans.setUid(IsMobileNumPhone.isUid());
				beans.setQuery_status("1");
				page_size++;
				myOrderPresenter.getInfo(beans);
				adapter.notifyDataSetChanged();
				toast("" + entity.getData());
			} else if (entity.getEventType() == myOrderPresenter.Delete_fail) {
				toast("" + entity.getData());
			}
		}
	}

	@Override
	public void onBtnSend(View v) {

		Intent intent = new Intent(getActivity(), ActivityPaymentMethod.class);
		intent.putExtra("price", beans.getList().get((Integer) v.getTag())
				.getPrice());
		intent.putExtra("orderNum", beans.getList().get((Integer) v.getTag())
				.getOrder_code());
		startActivity(intent);
	}

	@Override
	public void onCle(View v) {

		myOrderPresenter.getDelete(IsMobileNumPhone.isUid(), beans.getList()
				.get((Integer) v.getTag()).getOrder_code());
		beans.getList().remove((Integer) v.getTag());
		adapter.notifyDataSetChanged();
		Intent intent = new Intent("orderUpdate");
		getActivity().sendBroadcast(intent);

	}

	@Override
	public void onOkConfirm(View v) {

		myOrderPresenter.getBtnSendOrder(IsMobileNumPhone.isUid(), beans
				.getList().get((Integer) v.getTag()).getOrder_code());
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onRefresh() {

		showProgressDialog("数据加载中...", true);

		beans.setUid(IsMobileNumPhone.isUid());
		page_size = 1;
		beans.setQuery_status("1");
		myOrderPresenter.getInfo(beans);
	}

	@Override
	public void onLoadMore() {

		beans.setUid(IsMobileNumPhone.isUid());
		beans.setQuery_status("1");
		page_size++;
		myOrderPresenter.getInfo(beans);
	}

	@Override
	public void onWuliu(View v) {

	}

	public void onIsStop() {
		mylist.stopRefresh();
		mylist.stopLoadMore();
	}
}
